package com.csnf.servlet;

import com.csnf.mapper.OrderMapper;
import com.csnf.util.Order;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/getAllOrders")
public class OrderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String resource = "mybatis-config.xml";
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(resource));
        OrderMapper orderMapper = sqlSessionFactory.openSession(true).getMapper(OrderMapper.class);
        List<Order> allOrders = orderMapper.getAllOrders();
        req.setAttribute("Orders",allOrders);
        req.getRequestDispatcher("OrderList.jsp").forward(req,resp);
    }
}
